package com.zbkj.crmeb.member.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crmeb.core.exception.CrmebException;
import com.zbkj.crmeb.member.dao.MemberPointsManagementDao;
import com.zbkj.crmeb.member.model.MemberPointsManagement;
import com.zbkj.crmeb.member.service.IMemberPointsManagementService;
import com.zbkj.crmeb.store.model.ChannelIntegralConfig;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 会员积分管理 服务实现类
 * </p>
 *
 * @author xx
 * @since 2022-05-10
 */
@Service
public class MemberPointsManagementServiceImpl extends ServiceImpl<MemberPointsManagementDao, MemberPointsManagement> implements IMemberPointsManagementService {

    @Override
    public Boolean addOrUpd(MemberPointsManagement memberPointsManagement) {
        return this.saveOrUpdate(memberPointsManagement);
    }

    /**
     * 功能描述: 获取积分商城状态
     * @Param: [merId]
     * @Return: java.lang.Boolean
     * @Author: wangzh
     * @Date: 2023/2/20 16:12
     */
    @Override
    public Boolean getPointsEnableConfig(Integer merId) {
        MemberPointsManagement memberPointsManagement = this.getOne(Wrappers.<MemberPointsManagement>lambdaQuery()
                .eq(MemberPointsManagement::getMerId, merId)
                .last("limit 1"));
        if (Objects.isNull(memberPointsManagement)) {
            return false;
        }
        return memberPointsManagement.getPointsEnableFlag();
    }

    /**
     * 功能描述: 开启/关闭积分商城
     * @Param: [status, merId]
     * @Return: java.lang.Boolean
     * @Author: wangzh
     * @Date: 2023/2/28 14:36
     */
    @Override
    public Boolean setEnablePointsConfig(Boolean status, Integer merId) {

        if (merId == null || merId == 0) {
            return false;
        }
        return this.update(Wrappers.<MemberPointsManagement>lambdaUpdate()
                .eq(MemberPointsManagement::getMerId, merId)
                .set(MemberPointsManagement::getPointsEnableFlag, status));
    }

}
